﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Npgsql;
using System.Data.SqlClient;
using System.Data;

namespace CamadaAcessoBDPostgresql
{
    public class TaskConnection
    {
        public DataTable RunCommand(string pcommand, Boolean pretornar )
        {
            NpgsqlConnection cn = new NpgsqlConnection();
            try
            {
                cn.ConnectionString = DataConnection.StringDeConexao;

                NpgsqlCommand cmd = new NpgsqlCommand();
                cmd.Connection = cn;
                cmd.CommandText = pcommand;

                cn.Open();
                DataTable dt = new DataTable();

                if (pretornar)
                {
                    NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd);
                    da.Fill(dt);
                }
                else
                {
                    cmd.ExecuteNonQuery();
                }

                return dt;
                
            }
            catch (NpgsqlException ex)
            {
                throw new Exception("Erro DE SQL:" + ex.Message + "\n" + ex);
            }
            catch (Exception ex)
            {
                throw new Exception("Erro DE OUTRA EXCECAO:" + ex.Message);
            }
            finally
            {
                cn.Close();
            }


        }


    }
}
